<?php
/**
 * QingFeng licence
 *
 * @copyright  Copyright (c) 2005-2010 QingFeng Technologies Inc. (http://www.hnqss.cn)
 * @license  http://www.hnqss.cn/ SunMil License
 */

function theme_widget_qf_category(&$setting,&$env){
    // 判断是否首页
    /*if('site_ctl_default' == get_class($render)){
        $_returnData['isindex'] = true;
    }*/
	$_returnData['page'] = app::get('site')->router()->get_query_info('module');//当前页面
	
	//判断是否存在kvstore数据
    if( false&& base_kvstore::instance('b2c_goods')->fetch('goods_cat_ex_vertical_widget.data',$cat_list) ){
        return $cat_list;
    }
		
	/*====================取出所有品牌==========================*/
	$brand_mdl  = app::get('b2c')->model('brand');
    $brandlist = $brand_mdl->getAll();
	foreach ($brandlist as $key => $value) {
        $brand_list[$value['brand_id']] = $value;
    }
	$kvstore_goods_cat_expires = app::get('b2c')->getConf('kvstore_goods_cat_expires');
    $_returnData['brand_list'] = $brand_list;
	
	//取促销活动
	$salesList = _ex_vertical_getSales();
			
    $cat_mdl = app::get('b2c')->model('goods_cat');//商品分类接口 
    $cat_list =$cat_mdl->get_cat_list();//取出所有分类-无序
    $all_brandids = array();
    $all_cids = array();
    foreach ($cat_list as $k=>$cat) {
        switch ($cat['step']) {
        case 1:
            $all_cids 	= _ex_vertical_getAllChildAttr($cat_list,$cat['cat_id']);
            $all_cids[] 	= $cat['cat_id'];
            $all_typeids 	= _ex_vertical_getAllChildAttr($cat_list,$cat['cat_id'],'type');
            $all_typeids[] 	= $cat['type'];
            $all_brandids 	= _ex_vertical_getLinkBrandIds($all_typeids);			
            $cat['brand'] = $all_brandids;
			/*============关联促销=======================*/
            foreach ($salesList as $sale) {
                $allowLink = false;
                foreach ($sale['conditions']['conditions'] as $condition) {
                    $condition['value'] = $condition['value'] ? $condition['value'] : array();
                    switch ($condition['attribute']) {
                    case 'goods_cat_id':
                        $instersect = array_intersect($condition['value'],$all_cids);
                        if(count($instersect)>0){
                            $allowLink = true;
                        }
                        break;
                    case 'goods_brand_id':
                        $instersect = array_intersect($condition['value'],$all_brandids);
                        if(count($instersect)>0){
                            $allowLink = true;
                        }
                        break;
                    }
                }
                if($allowLink){
                    $cat['sales'][] = $sale;
                }
            }
			/*====================关联促销 end======================*/
			
			$cat['pic1'] = '';//广告图1
			$cat['pic2'] = '';//广告图2
			$cat['pic1link'] = '';//广告图1链接
			$cat['pic2link'] = '';//广告图2链接
			$cat['morelink'] = '';//重定义链接					
            $_returnData['data'][$cat['cat_id']] = $cat;
            break;
        case 2:
            $_returnData['data'][$cat['pid']]['lv2'][$cat['cat_id']] = $cat;
            break;
        case 3:
            $ids = explode(',',$cat['cat_path']);
            $_returnData['data'][$ids[1]]['lv2'][$cat['pid']]['lv3'][$cat['cat_id']] = $cat;
            break;
        }//end switch
    }
	
	if($setting['custom'] && is_array($setting['custom'])){
		foreach($setting['custom'] as $k=>$v){
			$_returnData['custom'][] = $v;
		}			
	}
	/*自定义内容插入*/
	foreach($setting['resetcat'] as $key=>$cat){
		/*if($cat['cat_name']){
			$_returnData['data'][$key]['cat_name'] = $cat['cat_name'];
		}*/
		if($cat['link']){
			$_returnData['data'][$key]['url'] = $cat['link'];
		}
		if($cat['pic1']){
			$_returnData['data'][$key]['pic1'] = $cat['pic1'];
		}
		if($cat['pic2']){
			$_returnData['data'][$key]['pic2'] = $cat['pic2'];
		}
		if($cat['link1']){
			$_returnData['data'][$key]['pic1link'] = $cat['pic1link'];
		}
		if($cat['link2']){
			$_returnData['data'][$key]['pic2link'] = $cat['pic2link'];
		}
		if($cat['morelink']){
			$_returnData['data'][$key]['morelink'] = $cat['morelink'];
		}
	}	
	//print_r($_returnData);exit;
    return $_returnData;
}

//获得商品分类
function _ex_vertical_getAllChildAttr($arr,$pid,$attribute = 'cat_id'){
    foreach ($arr as $item) {
        if(in_array($pid, explode(',', $item['cat_path']))){
            $_return[] = $item[$attribute];
        }
    }
    return $_return;
}

//获取关联品牌
function _ex_vertical_getLinkBrandIds($typeids){
    $sql = 'SELECT brand_id FROM '.kernel::database()->prefix.'b2c_type_brand WHERE type_id  in('.implode(',',array_unique($typeids)).')';
    $res =  app::get('b2c')->model('brand')->db->select($sql );
    foreach ($res as $key => $value) {
        $_return[] = $value['brand_id'];
    }
    if($_return){
        $_return = array_unique($_return);
    }
    return $_return;
}

//获取对应的促销活动
function _ex_vertical_getSales(){
    $goods_sales_mdl = app::get('b2c')->model('sales_rule_goods');
    $goods_sales_list = $goods_sales_mdl->getList('*');
    return $goods_sales_list;
}
?>